#include "Aplicacion.h"
#include "sdlScreen.h"
#include "sdlMusic.h"
#include "windowInit.h"
#include "singleSocket.h"
#include <jsoncpp/json.h>
#include "user.h"

#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define SCREEN_BPP 32

Aplicacion::Aplicacion(){
	this->monitor1 = sdlMonitor::getInstance();
	this->monitor2 = sdlMusicMonitor::getInstance();
	this->monitor3 = sdlTTFMonitor::getInstance();
}
void Aplicacion::correr(){
	bool quit = false;
	sdlScreen screen;
	screen.setName("N|N Crush");
	screen.initScreen(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,"icon.bmp");

	window* ventActual = new windowInit(screen);
	ventActual->show();

	sdlMusic music;
	music.load("loginMusic.wav");
	music.play();
	
	User* user = User::getInstance();

	SDL_Event event;
	while(!quit){
		if(SDL_PollEvent(&event)){
			window* w = NULL;
			if (ventActual->handleEvent(event,w)){
				if (w != NULL){
					delete ventActual;
					ventActual = w;
					ventActual->show();
				}
			} else {
				if (event.type == SDL_KEYDOWN){
					if (event.key.keysym.sym == SDLK_m){
						music.changeState();
					} else if (event.key.keysym.sym == SDLK_ESCAPE){
						quit = true;
					} else if (event.key.keysym.sym == SDLK_END){
						if (music.isPlaying()){
							music.stop();
						} else{
							music.play();
						}
					}
				} else if(event.type == SDL_QUIT){
					user->setName("");
					user->setPass("");
					quit = true;
				}
			}
		}
	}
	if (music.fadeOut(2500) && !music.isPaused()){
		SDL_Delay(2520);
	} else{
		music.stop();
		SDL_Delay(1500);
	}
}
Aplicacion::~Aplicacion(){
	sdlMonitor::eliminar();
	sdlMusicMonitor::eliminar();
	sdlTTFMonitor::eliminar();
	User::eliminar();
}
